package com.twohand.admin.Service.impl;

import com.qcloud.cos.model.Bucket;
import com.tencent.cloud.Response;
import com.twohand.admin.Service.CosService;
import com.twohand.admin.Util.CosUpload;
import com.twohand.admin.Util.CosUtil;
import com.twohand.admin.Vo.Cos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class CosServiceImpl implements CosService {
    @Autowired
    CosUpload cosUpload;

    @Override
    public Response getCre(String bucketName,String bucketOrigin) throws IOException {
        return cosUpload.getTempCred(bucketName,bucketOrigin);
    }

    @Override
    public List<Cos> getBucketList() {
        List<Bucket> buckets = CosUtil.getCosClient().listBuckets();
        List<Cos> cosList = new ArrayList<Cos>();
        for (Bucket bucket: buckets) {
            Cos cos = new Cos();
            cos.setName(bucket.getName());
            cos.setOrigin(bucket.getLocation());
            cos.setTag(bucket.getName() + "://");
            cos.setTarget("https://"+bucket.getName() + ".cos." + bucket.getLocation() + ".myqcloud.com");
            cosList.add(cos);
        }
        return cosList;
    }
}
